package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
import com.tmptSource.bean.OwBaseRoad;
import com.tmptSource.enums.*;
import com.tmptSource.mapper.OwBaseCommiteeMapper;
import com.tmptSource.bean.OwBaseCommitee;
import com.tmptSource.dto.OwBaseCommiteeDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.mapper.OwBaseRoadMapper;
import com.tmptSource.service.intf.IOwBaseCommiteeService;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="owBaseCommiteeService")
@Transactional(propagation = Propagation.REQUIRED)
public class OwBaseCommiteeService implements IOwBaseCommiteeService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired private DataLimitService dataLimitService;
    @Autowired private Mapper mapper;
    @Autowired private MyMapper myMapper;
    @Autowired private MyPageUtils myPageUtils;
    @Autowired
    private OwBaseCommiteeMapper owBaseCommiteeMapper;
    @Autowired
    private OwBaseRoadMapper owBaseRoadMapper;


    @Override
    public Result<OwBaseCommiteeDto> pagination(OwBaseCommiteeDto owBaseCommiteeDto,PageDto pageDto) {
        //分页查询
        Result<OwBaseCommiteeDto> result = myPageUtils.pagination(
                owBaseCommiteeDto,pageDto,
                OwBaseCommitee.class,
                OwBaseCommiteeDto.class,
                owBaseCommiteeMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param owBaseCommiteeDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<OwBaseCommiteeDto> pagination(OwBaseCommiteeDto owBaseCommiteeDto,PageDto pageDto,Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<OwBaseCommiteeDto> result = myPageUtils.pagination(
                owBaseCommiteeDto,pageDto,
                OwBaseCommitee.class,
                OwBaseCommiteeDto.class,
                owBaseCommiteeMapper,
                paramMap1,
                false,null);

        return result;
    }


    @Override
    public Result<OwBaseCommiteeDto> selectAll() {
        Result<OwBaseCommiteeDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<OwBaseCommitee> list = owBaseCommiteeMapper.selectAll(mapParam);
        List<OwBaseCommiteeDto> dtoList = myMapper.map(list, OwBaseCommiteeDto.class);
        result.setResultList(dtoList);

        return result.success();
    }


    @Override
    public Result<OwBaseCommiteeDto> queryCommiteeTree() {
        Result<OwBaseCommiteeDto> result = new Result();
        List<Map<String,Object>> commiteeList = new ArrayList<>();
        commiteeTree(commiteeList);//加载居委树List
        result.getResultMap().put("commiteeTree", commiteeList);
        return result.success();
    }


    /**
     * 居委的树结构
     * @return
     */
    private String commiteeTree(List<Map<String,Object>> commiteeList){

        JSONArray commiteeJA = new JSONArray();
        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page", pageDto);

        List<OwBaseCommitee> list = owBaseCommiteeMapper.selectAll(mapParam);
        for(OwBaseCommitee owBaseCommitee : list){

            long commiteeId = owBaseCommitee.getId();
            String commiteeName = owBaseCommitee.getCommiteeName();
            JSONObject commiteeJO = new JSONObject();
            commiteeJO.put("value",commiteeId);
            commiteeJO.put("text",commiteeName);
            Map<String,Object> commiteeMap = new HashMap<>();
            commiteeMap.put("value",commiteeId);
            commiteeMap.put("text",commiteeName);

            Map<String,Object> mapParamRoad = new HashMap<>();
            OwBaseRoad owBaseRoad = new OwBaseRoad();
            owBaseRoad.setOwBaseCommiteeId(commiteeId);
            mapParamRoad.put("bean",owBaseRoad);
            List<OwBaseRoad> owBaseRoadList = owBaseRoadMapper.selectAll(mapParamRoad);
            JSONArray roadJA = new JSONArray();
            List<Map<String,Object>> roadList = new ArrayList<>();
            for(OwBaseRoad owBaseRoad1 : owBaseRoadList){
                long roadId = owBaseRoad1.getId();
                String roadName = owBaseRoad1.getRoadName();
                JSONObject roadJO = new JSONObject();
                roadJO.put("value",roadId);
                roadJO.put("text",roadName);
                roadJA.put(roadJO);
                Map<String,Object> roadMap = new HashMap<>();
                roadMap.put("value",roadId);
                roadMap.put("text",roadName);
                roadList.add(roadMap);
            }
            commiteeJO.put("children",roadJA);
            commiteeJA.put(commiteeJO);
            commiteeMap.put("children",roadList);
            commiteeList.add(commiteeMap);
        }

        return commiteeJA.toString();
    }


    @Override
    public Result<OwBaseCommiteeDto> queryBaseDataTree() {
        Result<OwBaseCommiteeDto> result = new Result();
        List<Map<String,Object>> commiteeList = new ArrayList<>();
        commiteeTree(commiteeList);//加载居委树List
        result.getResultMap().put("commiteeTree", commiteeList);
        result.getResultMap().put("OwBuildingType", baseDataTransfer(OwBuildingType.toList()));
        result.getResultMap().put("OwDealType", baseDataTransfer(OwDealType.toList()));
        result.getResultMap().put("OwIssueDeclareStatus", baseDataTransfer(OwIssueDeclareStatus.toList()));
        result.getResultMap().put("OwOffpostEventType", baseDataTransfer(OwOffpostEventType.toList()));
        result.getResultMap().put("OwOffpostType", baseDataTransfer(OwOffpostType.toList()));
        result.getResultMap().put("OwPriorityCode", baseDataTransfer(OwPriorityCode.toList()));
        result.getResultMap().put("OwRegisterType", baseDataTransfer(OwRegisterType.toList()));
        result.getResultMap().put("OwSupportType", baseDataTransfer(OwSupportType.toList()));
        result.getResultMap().put("OwTaskType", baseDataTransfer(OwTaskType.toList()));
        result.getResultMap().put("OwTaskStatus", baseDataTransfer(OwTaskStatus.toList()));
        result.getResultMap().put("OwUseCode", baseDataTransfer(OwUseCode.toList()));
        result.getResultMap().put("OwUseType", baseDataTransfer(OwUseType.toList()));
        result.getResultMap().put("OwViolationType", baseDataTransfer(OwViolationType.toList()));
        result.getResultMap().put("OwWorkType", baseDataTransfer(OwWorkType.toList()));
        result.getResultMap().put("OwCmpnyBusinessMode", baseDataTransfer(OwCmpnyBusinessMode.toList()));
        result.getResultMap().put("OwCmpnyBusinessProperty", baseDataTransfer(OwCmpnyBusinessProperty.toList()));


        return result.success();
    }

    public List<Map<String,String>> baseDataTransfer(List<Map<String,String>> paramList){
        List<Map<String,String>> list = new ArrayList<>();

        for(Map<String,String> paramMap: paramList){
            Map<String,String> map = new HashMap<>();
            map.put("value",paramMap.get("keyName"));
            map.put("text",paramMap.get("valueName"));
            list.add(map);
        }

        return list;
    }

    @Override
    public Result<OwBaseCommiteeDto> selectById(long id) {
        Result<OwBaseCommiteeDto> result = new Result();

        OwBaseCommitee owBaseCommitee = owBaseCommiteeMapper.selectById(id);
        OwBaseCommiteeDto dto = mapper.map(owBaseCommitee,OwBaseCommiteeDto.class);
        result.setResultObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<OwBaseCommiteeDto> addData(OwBaseCommiteeDto owBaseCommiteeDto){
        Result<OwBaseCommiteeDto> result = new Result();

        //封闭数据
        OwBaseCommitee owBaseCommitee = mapper.map(owBaseCommiteeDto, OwBaseCommitee.class);
        owBaseCommitee.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owBaseCommiteeMapper.insert(owBaseCommitee);
        if(i==0){
            return result.fault("插入失败");
        }

        OwBaseCommiteeDto dto = mapper.map(owBaseCommitee,OwBaseCommiteeDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<OwBaseCommiteeDto> editData(OwBaseCommiteeDto owBaseCommiteeDto){
        Result<OwBaseCommiteeDto> result = new Result();

        //封装数据
        OwBaseCommitee owBaseCommitee = mapper.map(owBaseCommiteeDto, OwBaseCommitee.class);
        owBaseCommitee.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owBaseCommiteeMapper.updateById(owBaseCommitee);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        OwBaseCommiteeDto dto = mapper.map(owBaseCommitee,OwBaseCommiteeDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id,String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate",DateTool.getCurTimestamp());

        //调用mapper
        long i = owBaseCommiteeMapper.deleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }





}
